using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {
    public float health = 100;
    public float healthMax = 100;
    
    private int invulnerableTime = -1;

    private Vector2 healthBarPos = new Vector2(Screen.width * 0.9f,20);
    private Vector2 healthBarSize = new Vector2(100,32);
    
    public Texture2D healthBarEmpty;
    public Texture2D healthBarFull;
    
    // Use this for initialization
    void Start () {
    
    }
    
    void OnGUI() {
        // Health Bar Begin
        GUI.BeginGroup(new Rect(healthBarPos.x,healthBarPos.y,healthBarSize.x,healthBarSize.y));
        GUI.Box(new Rect(0,0,healthBarSize.x,healthBarSize.y), healthBarEmpty);
        
        GUI.BeginGroup(new Rect(0,0,healthBarSize.x * health / healthMax, healthBarSize.y));
        GUI.Box(new Rect(0,0,healthBarSize.x,healthBarSize.y), healthBarFull);
        GUI.EndGroup ();
        
        GUI.EndGroup ();
        // Health Bar End
    }
    
    
    // Update is called once per frame
    void Update () {
        invulnerableTime --;
        if (invulnerableTime < 0)
            return;
            // turn off invulnerability/flashing effect
            
        // health bar/meter update
        
        if (health < 0)
        {
            // what needs to be done upon death?
            GameObject.Destroy(gameObject);
        }
    }
    
    public void damage(float d){
        if (invulnerableTime < 0)
        {
            health -= d;
            invulnerableTime = 50;
        }
    }
}
